<template>
  <div class="dashboard-editor-container">
    <el-steps :active="active" align-center>
      <el-step :title="$t('uniqueTask.step1')" :description="$t('uniqueTask.step1Desc')"></el-step>
      <el-step :title="$t('regTask.submit_task')" :description="$t('regTask.submit_task_info')"></el-step>
    </el-steps>


    <template v-if="active == 1">
      <div style="margin-top: 40px;">
        <h3>{{$t('uniqueTask.fileList')}}</h3>
        <div style="margin-top: 20px;margin-bottom: 20px;color: #666666;">
          <el-alert
            :title="$t('uniqueTask.tips2')"
            type="info"
            :description="$t('uniqueTask.tips3')"
            show-icon
            :closable="false">
          </el-alert>
          <el-alert
            :title="$t('uniqueTask.tips4')"
            type="warning"
            :closable="false">
          </el-alert>
        </div>
        <el-upload
          class="upload-demo"
          ref="upload"
          accept="text/plain"
          :action="uploadUrl"
          :on-remove="handleRemove"
          :file-list="fileList"
          :auto-upload="false"
          :on-success="upsuccess"
        >
          <el-button slot="trigger" size="small" type="primary">
            <i class="el-icon-plus"></i>
            {{ $t('uniqueTask.addFile') }}
          </el-button>
          <el-button style="margin-left: 10px;"
                     size="small"
                     type="success"
                     @click="submitUpload"
                     v-loading.fullscreen.lock="fullscreenLoading"
                     :element-loading-text="$t('regTask.uploading')"
                     element-loading-spinner="el-icon-loading"
                     element-loading-background="rgba(0, 0, 0, 0.8)"
          >
            <i class="el-icon-upload"></i>
            {{ $t('regTask.uploadFile') }}
          </el-button>
          <div slot="tip" class="el-upload__tip">{{ $t('regTask.onlyTxt') }}</div>
        </el-upload>
      </div>
      <div style="width: 100%;text-align: right;padding-right: 20px;margin-top: 40px;">
        <el-button @click="to2()" type="primary">{{ $t('regTask.next') }}</el-button>
      </div>
    </template>

    <template v-if="active==2">
      <div style="margin-top: 40px;">
        {{ $t('regTask.taskName') }}:
        <el-input style="width: 220px;" v-model="jobname" :placeholder="$t('regTask.taskNameInput')"></el-input>
      </div>
      <div style="margin-top: 40px;">
        <el-radio-group v-model="add91">
          <el-radio :label="0">{{ $t('uniqueTask.nadd91') }}</el-radio>
          <el-radio :label="1">{{ $t('uniqueTask.add91') }}</el-radio>
        </el-radio-group>
      </div>
      <div style="margin-top: 40px;">
        <el-radio-group v-model="split">
          <el-radio :label="0">{{ $t('uniqueTask.notSplit') }}</el-radio>
          <el-radio :label="1">{{ $t('uniqueTask.autoSplit') }}</el-radio>
        </el-radio-group>
      </div>
      <div style="margin-top: 40px;" v-if="split == 1">
        {{ $t('explodeFile.splitNum') }}:
        <el-input type="number" style="width: 220px;" v-model="splitNum" ></el-input>
      </div>
      <div style="width: 100%;margin-top: 40px;text-align: center;padding-right: 20px;">
        <el-button @click="active--">{{ $t('regTask.pre') }}</el-button>
        <el-button @click="submitJob" type="primary">{{ $t('regTask.submit') }}</el-button>
      </div>
    </template>

  </div>
</template>

<script>
import { create, getAllPlatform } from '@/api/jobs'

export default {
  name: 'createuniquephone',
  created() {
    let _this = this
    this.uploadUrl = process.env.VUE_APP_BASE_API + '/upload/file'
    getAllPlatform({ type: 0 }).then(res => {
      if (res.code == 0) {
        _this.allPlatform = res.data
      }
    })
  },
  data() {
    return {
      uploadUrl: '',
      allPlatform: [],
      length: 0,
      active: 1,
      fileList: [],
      files: [],
      source: 1,
      sourceDate: '',
      fullscreenLoading:false,
      jobname: '',
      split:0,
      splitNum:50000,
      add91:0
    }
  },
  methods: {
    submitUpload() {
      this.fullscreenLoading = true;
      this.$refs.upload.submit()
    },
    handleRemove(file, fileList) {
      this.fileList = fileList
      console.log(fileList)
    },
    upsuccess(response, file, fileList) {
      this.fullscreenLoading = false;
      if (response.code != 0) {
        this.$notify.error({
          title: '文件有误',
          message: '仅支持txt文件,请重新选择上传文件！'
        })
        this.$refs.upload.clearFiles()
      }

      this.$notify.success({
        title: this.$t('regTask.upsuccess'),
        message: this.$t('regTask.file') + '[' + file.name + ']' + this.$t('regTask.upsuccess2')
      })

      file.url = response.data
      this.fileList = fileList
    },
    to2() {
      if (this.fileList.length == 0) {
        this.$message.error(this.$t('uniqueTask.tips1'))
        return
      }
      this.files = []
      try {
        this.fileList.forEach(item => {
          if (item.url && item.url != '') {
            this.files.push(item)
          } else {
            throw new Error(this.$t('regTask.tips9'))
          }
        })
      } catch (e) {
        this.$message.warning(e.message)
        return
      }
      this.length = this.files.length
      this.to3()
    },
    to3() {
      this.active++
    },
    datechange() {
      console.log(this.sourceDate)
    },
    submitJob() {

      if (this.jobname == '') {
        this.$message.warning(this.$t('regTask.taskNameInput'))
        return
      }

      var source = []
      this.files.forEach(item => {
        let tmp = {
          raw: item.url,
          alias: item.name
        }
        source.push(tmp)
      })

      let data = {
        jobName: this.jobname,
        sourceFile: source,
        type: 1
      }

      if(this.split == 1){
        data['splitNum'] = Math.abs(this.splitNum);
      }

      if(this.add91 == 1){
        data['add91'] = 1;
      }

      const loading = this.$loading({
        lock: true,
        text: this.$t('v520.createing'),
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      create(data).then(res => {
        loading.close()
        if (res.code == 0) {
          this.$message.success(this.$t('v520.createSuccess'))
          this.$notify.success({ title: this.$t('v520.tips'), message: this.$t('v520.waitResult') })
          this.$store.dispatch('tagsView/delView', this.$route)
          this.$router.go(-1)
        } else {
          this.$message.error(res.msg)
          return
        }
      }).catch(e => {
        loading.close()
        this.$message.error('请求失败！')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  background-color: #FFF;
  min-height: 100vh;
  padding: 50px 60px 0px;

  .pan-info-roles {
    font-size: 12px;
    font-weight: 700;
    color: #333;
    display: block;
  }

  .info-container {
    position: relative;
    margin-left: 190px;
    height: 150px;
    line-height: 200px;

    .display_name {
      font-size: 48px;
      line-height: 48px;
      color: #212121;
      position: absolute;
      top: 25px;
    }
  }
}
</style>
<style>
.alert_box {
  min-width: 320px;
  width: auto;
  margin-top: 15px;
  background-color: #f0f9eb;
  color: #67c23a;
  padding: 8px 16px;
  border-radius: 4px;
  display: block;
}

.aside {
  background: #eef1f6;
  padding: 8px 24px;
  margin-bottom: 20px;
  border-radius: 2px;
  display: block;
  line-height: 32px;
  font-size: 16px;
  font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
  color: #2c3e50;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style>
